package com.bw.flinkstreaming.transform1;

import com.bw.flinkstreaming.source1.job2.NotParallelSource;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;

public class UnionTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //数据源1
        DataStreamSource<Long> data1 = env.addSource(new NotParallelSource()).setParallelism(1);

        //数据源2
        DataStreamSource<Long> data2 = env.addSource(new NotParallelSource()).setParallelism(1);

        //把data1和data2合并到一起，数据类型必须一致
        DataStream<Long> dataAll = data1.union(data2);

        SingleOutputStreamOperator<Long> result = dataAll.map(new MapFunction<Long, Long>() {
            @Override
            public Long map(Long value) {
                System.out.println("接受到的数据: " + value);
                return value;
            }
        });
        SingleOutputStreamOperator<Long> sum = result.timeWindowAll(Time.seconds(2)).sum(0);
        sum.print().setParallelism(1);
        env.execute("UnionTest");


    }
}
